package com.cloudlibrary.mapper;

import com.cloudlibrary.domain.Book;
import com.cloudlibrary.domain.User;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository
public interface BookMapper {
    @Select("select * from book order by book_uploadtime desc ")
    @Results(id = "BookMap", value = {
            //id字段默认为false，表示不是主键
            //column表示数据库表字段，property表示持久化类属性名称。
            @Result(id = true, column = "book_id", property = "id"),
            @Result(column = "book_name", property = "name"),
            @Result(column = "book_isbn", property = "isbn"),
            @Result(column = "book_press", property = "press"),
            @Result(column = "book_author", property = "author"),
            @Result(column = "book_pagination", property = "pagination"),
            @Result(column = "book_price", property = "price"),
            @Result(column = "book_uploadtime", property = "uploadTime"),
            @Result(column = "book_status", property = "status"),
            @Result(column = "book_borrower", property = "borrower"),
            @Result(column = "book_borrowtime", property = "borrowTime"),
            @Result(column = "book_returntime", property = "returnTime"),
    })
    //通过时间查询
    Page<Book> selectNewBooks();

    @Select("SELECT * FROM book where book_id = #{id}")
    @ResultMap("BookMap")
    //根据id查询图书信息
    Book findById(@Param("id") String id);

    @Update("UPDATE book SET " +
            "book_name = #{name}, book_isbn = #{isbn}, " +
            "book_press = #{press}, book_author = #{author}, " +
            "book_pagination = #{pagination}, book_price = #{price}, " +
            "book_uploadtime = #{uploadTime}, book_status = #{status}, " +
            "book_borrower = #{borrower}, book_borrowtime = #{borrowTime}, " +
            "book_returntime = #{returnTime} " +
            "WHERE book_id = #{id}")
    //编辑图书信息
    Integer editBook(Book book);

    @Select({"<script>" +
            "SELECT * FROM book " +"where book_status !='3'" +
            "<if test='name != null'> "+
            "AND  book_name  like  CONCAT('%',#{name},'%')</if>" +
            "<if test='press != null'>" +
            "AND book_press like  CONCAT('%', #{press},'%') </if>" +
            "<if test='author != null'>" +
            "AND book_author like  CONCAT('%', #{author},'%')</if>" +
            "order by book_status" +
            "</script>"})
    @ResultMap("BookMap")
    // 查询图书
    Page<Book> searchBooks(Book book);

    @Insert("INSERT INTO `book`(`book_id`, `book_name`, `book_isbn`, `book_press`, `book_author`, `book_pagination`, `book_price`, `book_uploadtime`, `book_borrower`, `book_borrowtime`, `book_returntime`) VALUES (NULL, #{name}, #{isbn}, #{press}, #{author}, #{pagination}, #{price}, #{uploadTime}, NULL, NULL, NULL)")
    //新增图书
    Integer addBook(Book book);

    /*查询当前用户未归还的图书信息*/
    @Select({"<script>" +
            "SELECT * FROM book " +"where book_borrower = #{borrower}" +
            "<if test='name != null'> "+
            "AND  book_name  like  CONCAT('%',#{name},'%')</if>" +
            "<if test='press != null'>" +
            "AND book_press like  CONCAT('%', #{press},'%') </if>" +
            "<if test='author != null'>" +
            "AND book_author like  CONCAT('%', #{author},'%')</if>" +
            "order by book_status" +
            "</script>"})
    @ResultMap("BookMap")
    Page<Book> selectMyBorrowed(Book book);

    /*查询当前用户未归还和所有待确认归还的图书信息。*/
    @Select({"<script>" +
            "SELECT * FROM book " +"where book_borrower is not null and book_borrower != ''" +
            "<if test='name != null'> "+
            "AND  book_name  like  CONCAT('%',#{name},'%')</if>" +
            "<if test='press != null'>" +
            "AND book_press like  CONCAT('%', #{press},'%') </if>" +
            "<if test='author != null'>" +
            "AND book_author like  CONCAT('%', #{author},'%')</if>" +
            "order by book_status" +
            "</script>"})
    @ResultMap("BookMap")
    Page<Book> selectBorrowed(Book book);


}
